import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import math


def _on_press(event):
    print("Position:", event.button, event.xdata, event.ydata)


def img_mouse(pic):
    fig = plt.figure()
    img = Image.open(pic)

    plt.imshow(img, animated=True)

    fig.canvas.mpl_connect("button_press_event", _on_press)
    plt.show()


def d2g(dim: tuple, center: list, sigma=[[1, 0], [0, 1]]):

    gaussian_matrix = np.zeros((dim[0], dim[1]))

    if not center:
        c = np.array([dim[0] // 2, dim[1] // 2])
    else:
        c = np.array(center)

    sigma = np.array(sigma)
    theta = 1 / (2 * math.pi * (np.linalg.det(sigma)) ** (1 / 2))

    for i in range(dim[0]):
        for j in range(dim[1]):
            gaussian_matrix[i][j] = theta * np.exp(
                -(1 / 2)
                * np.array([np.array([j, i]) - c])
                @ np.linalg.inv(sigma)
                @ np.array([np.array([j, i]) - c]).T
            )

    return gaussian_matrix
